﻿using Emoney.UserTraining.BLL;
using Emoney.UserTraining.Cache;
using Emoney.UserTraining.Comm;
using Emoney.UserTraining.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Emoney.UserTraining.Web.Handle
{
    /// <summary>
    /// TrainSoftTip 的摘要说明
    /// </summary>
    public class TrainSoftTip : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string querystring = context.Request.QueryString.ToString();
            string uname = "";
            GetUserName.access access = new GetUserName.access();
            var user = access.GetUserInfo(querystring, "127.0.0.1", 0);
            if (!string.IsNullOrEmpty(user.UserName))
            {
                uname = user.UserName;
            }
            string uid = context.Request["uid"];
            string pid = context.Request["pid"];
            string agentid = context.Request["agentid"];
            string ct = context.Request["clienttype"];
            string area = "";

            pid = Pub.GetPid_9To3(pid);

            emoneybusiness.emoneybusiness ws = new emoneybusiness.emoneybusiness();
            var tt = ws.GetCityName(OTO.ToStr(agentid, "100000000"));
            if (tt == null)
            {
                area = "上海";
            }
            else
            {
                area = tt.Trim();
            }
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(ct))
            {
                if (ct == "3")
                {
                    if (pid != "211")
                    {
                        context.Response.Write("");
                        context.Response.End();
                        return;
                    }
                }
            }
            //开课提醒(软件调用返回json数据)
            sb.Append(Get开课提醒(uid, uname));
            sb.Append(Get课程预告(area, pid));
            context.Response.Write(sb.ToString());
            context.Response.End();
        }

        private string Get课程预告(string area,string pid)
        {
            News2CacheTools cache = new News2CacheTools();
            string key = "getkcyg_" + area.Replace(" ","") + pid;
            string str=cache.Get<string>(key);
            if (string.IsNullOrEmpty(str))
            {
                StringBuilder sb = new StringBuilder();
                List<Traning_NetworkMeeting> lstkcyg = GetKCYG(area, pid);
                var i = 1;
                foreach (var c in lstkcyg)
                {
                    sb.Append("{|");
                    sb.Append("id[" + c.Id + "]");
                    sb.Append("type[2]");
                    sb.Append("title[《" + StringTools.SubStr(c.mtg_name,13) + "》]");
                    sb.Append("date[" + c.class_date.ToString("yyyyMMdd") + "]");
                    sb.Append("time[" + c.class_date.ToString("HHmm") + "00]");
                    sb.Append("endtime[" + c.class_date.AddMinutes(Double.Parse(c.Duration_date)).ToString("HHmm") + "00]");
                    sb.Append("beforetime[60]");
                    sb.Append("url[]");
                    sb.Append("|}");
                    i++;
                }
                str=sb.ToString();
                cache.Add(key, str, 0, 0, 20);
            }
            return str;
        }

        private string Get开课提醒(string uid,string uname)
        {
            TrainNetwoekAndUserCache tCache = new TrainNetwoekAndUserCache();
            string str= tCache.GetCache(uid);
            if (string.IsNullOrEmpty(str))
            {
                List<Training_SoftTip> lsttip = new List<Training_SoftTip>();
                Training_MeetingUserBLL bll = new Training_MeetingUserBLL();
                List<TrainNetwoekAndUser> lstkktx = bll.GetLstByUid(uid).Where(x => x.end_date > DateTime.Now).OrderBy(x => x.class_date).ToList();
                StringBuilder sb = new StringBuilder();
                foreach (var c in lstkktx)
                {
                    sb.Append("{|");
                    sb.Append("id[" + c.Id + "]");
                    sb.Append("type[1]");
                    sb.Append("title[《" + StringTools.SubStr(c.mtg_name, 13) + "》]");
                    sb.Append("date[" + c.class_date.ToString("yyyyMMdd") + "]");
                    sb.Append("time[" + c.class_date.ToString("HHmm") + "00]");
                    sb.Append("endtime[" + c.class_date.AddMinutes(Double.Parse(c.Duration_date)).ToString("HHmm") + "00]");
                    sb.Append("beforetime[" + c.before_time + "]");
                    sb.Append("url[http://ds.emoney.cn/train/OpenMeeting/open.aspx?mtg_id=" + c.mtg_id + "&name=" + uname + "]");
                    sb.Append("|}");
                }
                str=sb.ToString();
                tCache.AddCache(uid, str);
            }
            return str;
        }
    
        private List<Traning_NetworkMeeting> GetKCYG(string area,string pid)
        {
            List<Traning_NetworkMeeting> lstOne = null;
            List<Traning_NetworkMeeting> list = null;
            Traning_NetworkMeetingBLL bll = new Traning_NetworkMeetingBLL();
            list = bll.GetNetAll();
            List<Traning_NetworkMeeting> lstarea = list.Where(x => x.ddlarea == area).Where(x => x.chblist_pro.Contains(pid)).ToList();
            List<Traning_NetworkMeeting> lstqg = list.Where(x => x.ddlarea == "全国").Where(x => x.chblist_pro.Contains(pid)).ToList();
            lstOne = lstarea.Concat(lstqg).Where(x => x.class_date > DateTime.Now).OrderBy(x => x.class_date).ToList();//查询未开始的两条课程给软件
            return lstOne;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}